package com.project.controller; /**
 * @author wzm
 * @create 2023-05-31 9:42
 */

import com.alibaba.fastjson.JSON;
import com.project.entity.PageBean;
import com.project.entity.Student;
import com.project.service.StudentService;
import com.project.service.impl.StudentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/student/*")
public class StudentServlet extends BaseServlet {

    private StudentService studentService = new StudentServiceImpl();

    /*
      登录的方法
     */
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");

        //获取请求体中的数据
        BufferedReader reader = request.getReader();
        String s = reader.readLine();

        //将字符串转为对象
        Student student = JSON.parseObject(s, Student.class);

        //调用Service进行登录验证
       Student dataStu = studentService.login(student);

        PrintWriter writer = response.getWriter();

        //将页面对象和查询到的数据库对象进行对比
        if(dataStu  == null){
            //用户名不存在
            writer.write("-1");
         return;
        }

        if(!student.getPassword().equals(dataStu.getPassword())){
            //密码错误
            writer.write("-2");
            return;
        }

        //用户名和密码都是通过的
        writer.write(dataStu.getUsername());

    }

    //查询全部数据
    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");

        //查询全部
        List<Student> all = studentService.getAll();
        response.getWriter().write(JSON.toJSONString(all));
    }

    //新增
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        String s = request.getReader().readLine();

        //字符串转为可以使用的对象
        Student student = JSON.parseObject(s, Student.class);
        boolean add = studentService.add(student);
        response.getWriter().write(add?"success":"error");


    }

    //删除
    protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        String id = request.getParameter("id");
        Student student = new Student();
        student.setId(Integer.parseInt(id));

        boolean remove = studentService.remove(student);
        response.getWriter().write(remove?"success":"error");


    }

    //根据id查询getById
    protected void getById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        String id = request.getParameter("id");
        Student student = new Student();
        student.setId(Integer.parseInt(id));

        Student byId = studentService.getById(student);
        //将对象转为json响应给ajax
        response.getWriter().write(JSON.toJSONString(byId));
    }

    //修改信息
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        String s = request.getReader().readLine();

        //字符串转为可以使用的对象
        Student student = JSON.parseObject(s, Student.class);
        boolean modify = studentService.modify(student);
        response.getWriter().write(modify?"success":"error");

    }


    //批量删除 deleteBatch
    protected void deleteBatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        String s = request.getReader().readLine();
        //将字符串转为对象形式
        int[] ids = JSON.parseObject(s, int[].class);

       boolean flag = studentService.removeByIds(ids);

        response.getWriter().write(flag?"success":"error");


    }

    //分页查询全部findAllbyPage
    protected void findAllbyPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");

        //获取当前页  显示的条数
        int currentPage = Integer.parseInt(request.getParameter("currentPage"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        //通过service调用分页查询的方法 list+count
       PageBean<Student> bean = studentService.selectByPage(currentPage,pageSize);
        System.out.println("bean = " + bean);

        response.getWriter().write(JSON.toJSONString(bean));

    }

    protected void aaa(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    //带模糊条件查询和分页的方法findAllbyPageIf
    protected void findAllbyPageIf(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");

        //获取当前页  显示的条数
        int currentPage = Integer.parseInt(request.getParameter("currentPage"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        //接收条件
        BufferedReader reader = request.getReader();
        //将条件封装为一个对象
        Student student = JSON.parseObject(reader.readLine(), Student.class);


        //通过service调用分页查询的方法 list+count  带条件
        PageBean<Student> bean = studentService.selectByPageIf(currentPage,pageSize,student);
        System.out.println("bean = " + bean);

        response.getWriter().write(JSON.toJSONString(bean));

    }


}
